import http.*
import korlibs.image.color.*
import korlibs.image.format.*
import korlibs.io.file.std.*
import korlibs.korge.input.*
import korlibs.korge.scene.*
import korlibs.korge.service.storage.*
import korlibs.korge.ui.*
import korlibs.korge.view.*
import korlibs.math.geom.*

class HousesScene : Scene() {
    override suspend fun SContainer.sceneMain() {
        val houses = listOf("Gryffindor", "Slytherin", "Hufflepuff", "Ravenclaw")
        houses.forEach { house -> repeatSendPoints(house) }

        uiGridFill(cols = 2, rows = 2, size = Size(views.virtualWidth, views.virtualHeight)) {
            houses.forEach { house ->
                uiButton("", icon = resourcesVfs["$house.png"].readBitmapSlice()) {
                    background.apply { colorMul = Colors.BLACK }
                }.onClick { sceneContainer.changeTo { HouseScene(house) } }
            }
        }
    }

    private suspend fun repeatSendPoints(house: String) {
        val httpClient = PointsHttpClient(storage["serverIp"], storage["id"])

        storage.getOrNull(house)?.let {
            httpClient.addPoints(house, it.toInt())
        }
    }
}
